package com.py.credit.service.impl;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.dao.TemplateDao;
import com.py.credit.entity.Template;
import com.py.credit.entity.TemplateDetailData;
import com.py.credit.service.TemplateService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class TemplateServiceImpl implements TemplateService {

	@Autowired
	private TemplateDao templateDao;

	@Override
	public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
			String conditionStr, Page page) {
		List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
		List<Map<String, Object>> template = templateDao.selectByConditionForPage(columns, additionalColumns,
				conditions, page);

		return template;
	}

	@Override
	public List<Map<String, Object>> selectMbCode() {
		return templateDao.selectMbCode();
	}

	@Override
	public int selectSameName(String id, String name) {
		return templateDao.selectSameName(id, name);
	}

	@Override
	public void addTemplate(String name, String content, String type, int managerId, String start_time,
			String end_time) {
		templateDao.addTemplate(name, content, type, managerId, start_time, end_time);

	}

	@Override
	public void del(String ids) {
		templateDao.del(Arrays.asList(ids.split(",")));

	}

	@Override
	public void modify(String id, String name, String content, String start_time, String end_time) {
		templateDao.modify(id, name, content, start_time, end_time);

	}

	public void insertTemplateData(int m_id, String start_time, String end_time) {
		Map<String, String> map = templateDao.selectTemplateData(start_time, end_time);
		String warehous_proportion = (Integer.parseInt(map.get("rukushu")) / Integer.parseInt(map.get("tcount"))) * 100
				+ "%";

		// 报送单位（政府部门）数:
		int dept_government_num = templateDao.selectDeptGovernmentnum(start_time, end_time);

		// 报送单位（行业协会）数:
		int dept_association_num = templateDao.selectAssociationnum(start_time, end_time);

		// 报送单位数（专业机构）:
		int dept_mechanism_num = templateDao.selectMechanismnum(start_time, end_time);

		int id = templateDao.insertTemplateData(map.get("tcount").toString(), map.get("rukushu").toString(),
				warehous_proportion, map.get("num").toString(), dept_government_num, dept_association_num,
				dept_mechanism_num, m_id);
		List<TemplateDetailData> list = templateDao.selectTemplateDetailData(start_time, end_time);

		for (Iterator<TemplateDetailData> it = list.listIterator(); it.hasNext();) {
			TemplateDetailData next = it.next();
			String success_proportion = (next.getSuccess_num() / next.getTotal_num()) * 100 + "%";

			templateDao.insertTemplateDetailData(next.getRegion_name(), next.getDept_top_name(), next.getTable_name(),
					next.getDept_name(), next.getTotal_num(), next.getSuccess_num(), next.getError_num(),
					success_proportion, id);
		}

	}

	@Override
	public Template selectOne(int id) {
		return templateDao.selectOne(id);
	}

	@Override
	public int selectSameType(String type) {
		return templateDao.selectSameType(type);
	}

	@Override
	public void startEnable(int id) {
		templateDao.startEnable(id);

	}
}
